//定义需要用到的请求地址
var replylist_url = mlog.uc.spaceUrl + "/t/replylist";
var add_url = mlog.uc.spaceUrl + "/admin/t/add";
var delete_url = mlog.uc.spaceUrl + "/admin/t/delete";
var deleteReply_url = mlog.uc.spaceUrl + "/admin/t/deleteReply";
var reply_url = mlog.uc.spaceUrl + "/admin/t/reply";

/**
 * 插入Twitter
 */
function addTwitter(){
	if(mlog.uc.mine === false){
		alert('无权操作');
		return;
	}
	$('#twitterPostForm').submit();
}

/**
 * 删除Twitter
 * @param id
 */
function delTwitter(id){
	if(!id)return;
	if(mlog.uc.mine === false){
		alert('无权操作');
		return;
	}
	$.post(delete_url + '?id=' + id, function(response){
		if(response.success){
			$('#twitter_' + id).fadeOut();
		}
		else{
			alert(response.message);
		}
	});
}

/**
 * 删除回复
 * @param id
 */
function delReply(id){
	if(!id)return;
	if(mlog.uc.mine === false){
		alert('无权操作');
		return;
	}
	$.post(deleteReply_url + '?id=' + id, function(response){
		if(response.success){
			$('#reply_' + id).fadeOut();
		}
		else{
			alert(response.message);
		}
	});
}

/**
 * 显示回复框
 * 
 * @param twitter
 */
function showReply(twitter){
	if(!twitter){
		return;
	}
	var display = $('#t_reply_' + twitter).css('display');
	if(display == 'none'){
		$('#t_reply_' + twitter).fadeIn();
		$('#reply_txt_' + twitter).focus();
		$('#reply_txt_' + twitter).focusEnd();
		showReplyList(twitter);
	}
	else if(display == 'block'){
		$('#t_reply_' + twitter).fadeOut();
	}
}

/**
 * 回复
 * 
 * @param twitter
 */
function replyTwitter(twitter){
	if(!twitter){
		return;
	}
	var txt = document.getElementById('reply_txt_' + twitter);
	if(!txt){
		return;
	}
	var content = txt.value;
	if(!content || !$.trim(content)){
		alert('请输入回复内容');
		return;
	}
	$.post(reply_url, {twitter : twitter, content: content}, function(response){
		if(response.success){
			showReplyList(twitter);
			txt.value = '';
		}
		else {
			alert(response.message);
		}
	});
}

/**
 * 回复用户评论
 * @param twitter twitter编号
 * @param reply twitterreply编号
 * @param authorAlias 被回复人的昵称
 */
function reply_comment(twitter, reply, authorAlias){
	if(!twitter){
		return;
	}
	var txt = document.getElementById('reply_txt_' + twitter);
	if(!txt){
		return;
	}
	txt.value = '@' + authorAlias + ' : ';
	$(txt).focus();
	$(txt).focusEnd();
}

/**
 * 显示最近的10条回复
 * @param twitter
 */
function showReplyList(twitter){
	if(!twitter){
		return;
	}
	var url = replylist_url + '?twitter=' + twitter;
	$.post(url, function(response){
		if(response.success){
			var replys = response.data.replys;
			var html = '';
			if(replys.length > 0){
				for(var i = 0; i < replys.length; i++){
					var deleteLabel = '';
					if(mlog.uc.mine === true){
						deleteLabel = '<a href="javascript:delReply(' + replys[i].id + ');">删除</a>';
					}
					var replyHtml = [
						'<div class="media" id="reply_' + replys[i].id + '">',
		        		'	<a href="#" class="media-cap"><img src="' + m.smallAvatar(replys[i].author.smallAvatar) + '" class="img-radius"></a>',
		        		'	<div class="media-body">',
		        		'		<div class="media-body-title">',
		        		'			<strong><a href="' + m.spaceUrl(replys[i].author.name) + '" target="_blank">' + replys[i].author.alias + '</a>：</strong>',
		        		'		</div>',
		        		'		<p class="typo-small">' + resolveEmotion(replys[i].content) + '</p>',
		        		'		<p class="typo-small">',
		        		'			发表于：' + replys[i].createTime,
	        			'			<a href="javascript:reply_comment(' + twitter + ', ' + replys[i].id + ', \'' + replys[i].author.alias + '\');">回复</a>&nbsp;&nbsp;',
	        			deleteLabel,
		        		'		</p>',
		        		'	</div>',
		        		'</div>'
					].join('');
					html += replyHtml;
				}
				if(replys.length == 10){
					html += '<div style="text-align:center;"><a href="javascript:void(0);">查看更多&gt;&gt;</a></div>';
				}
			}
			else {
				html = [
					'<div style="text-align:center;font-size:15px;font-weight:bold;">暂无评论，赶紧来抢个沙发吧。</div>'
				].join('');
			}
			$('#reply_div_' + twitter).html(html);
		}
	});
}

$(document).ready(function(){
	//内容框ctrl+enter
	$('#content').ctrlEnter(function(){
		$('#twitterPostForm').submit();
	});
	
	//回复框ctrl+enter
	$('.t_reply_txt').ctrlEnter(function(){
		var twitter = $(this).data('id');
		if(twitter){
			replyTwitter(twitter);
		}
	});
	
	//解析表情
	$('.twitter-content').each(function(){
		var html = $(this).html();
		if(html){
			$(this).html(resolveEmotion(html));
		}
	});

	//发表twitter ajaxform
	$('#twitterPostForm').ajaxForm({
		success : function(response){
			if(response.success){
				$('#content').val('');
				var t = response.data.twitter;
				var html = [
					'<div class="media" id="twitter_' + t.id + '" style="display:none;">',
	        		'	<a href="#" class="media-cap"><img src="' + m.smallAvatar(t.author.smallAvatar) + '" class="img-radius"></a>',
	        		'	<div class="media-body">',
	        		'		<div class="media-body-title">',
	        		'			<strong><a href="' + m.spaceUrl(t.author.name) + '" target="_blank">' + t.author.alias + '</a>：</strong>',
	        		'		</div>',
	        		'		<p class="typo-p twitter-content">',
	        		'			' + resolveEmotion(t.content),
	        		'		</p>',
	        		'		<p class="typo-small">',
	        		'			发表于：' + t.createTime,
	        		'			<a href="javascript:showReply(' + t.id + ', \'' + t.author.alias + '\');">回复</a>',
	        		'			<a href="javascript:delTwitter(' + t.id + ');">删除</a>',
	        		'		</p>',
	        		'	</div>',
		        	'	<div class="t_reply" id="t_reply_' + t.id + '">',
					'		<div class="arrow">',
					'		</div>',
					'		<div>',
			    	'			<textarea id="reply_txt_' + t.id + '" data-id="' + t.id + '" class="t_reply_txt" placeholder="回复' + t.author.alias + '"></textarea>',
			    	'			<ul class="tool">',
				    '           	<li class="left" id="reply_eminsert_' + t.id + '">',
				    '            		<a class="emotion" hidefocus="hidefocus" href="javascript:showEmotion(' + t.id + ', \'reply_eminsert_' + t.id + '\', \'reply_txt_' + t.id + '\');">表情</a>',
				    '            	</li>',
				    '            	<li class="right"><input type="button" class="button button-inline" value="回复" onclick="replyTwitter(' + t.id + ');" /></li>',
				    '            </ul>',
					'		</div>',
					'		<div id="reply_div_' + t.id + '">',
					'		</div>',
					'	</div>',
	        		'</div>',
				].join("");
				var medias = $('#medias').children('.media');
				if(medias.length > 0){
					$(medias[0]).before(html);
				}
				else {
					$('#listpub').fadeOut();
					$('#medias').append(html);
				}
				//动画展示
				$('#twitter_' + t.id).fadeIn();
			}
			else{
				alert(response.message);
			}
		}
	});
});


var ems = [{"id":"14", "phrase":"微笑", "url":"default/14.gif"},{"id":"1", "phrase":"撇嘴", "url":"default/1.gif"},{"id":"2", "phrase":"色", "url":"default/2.gif"},{"id":"3", "phrase":"发呆", "url":"default/3.gif"},{"id":"4", "phrase":"得意", "url":"default/4.gif"},{"id":"5", "phrase":"流泪", "url":"default/5.gif"},{"id":"6", "phrase":"害羞", "url":"default/6.gif"},{"id":"7", "phrase":"闭嘴", "url":"default/7.gif"},{"id":"8", "phrase":"睡", "url":"default/8.gif"},{"id":"9", "phrase":"大哭", "url":"default/9.gif"},{"id":"10", "phrase":"尴尬", "url":"default/10.gif"},{"id":"11", "phrase":"发怒", "url":"default/11.gif"},{"id":"12", "phrase":"调皮", "url":"default/12.gif"},{"id":"13", "phrase":"呲牙", "url":"default/13.gif"},{"id":"0", "phrase":"惊讶", "url":"default/0.gif"},{"id":"15", "phrase":"难过", "url":"default/15.gif"},{"id":"16", "phrase":"酷", "url":"default/16.gif"},{"id":"96", "phrase":"冷汗", "url":"default/96.gif"},{"id":"18", "phrase":"抓狂", "url":"default/18.gif"},{"id":"19", "phrase":"吐", "url":"default/19.gif"},{"id":"20", "phrase":"偷笑", "url":"default/20.gif"},{"id":"21", "phrase":"可爱", "url":"default/21.gif"},{"id":"22", "phrase":"白眼", "url":"default/22.gif"},{"id":"23", "phrase":"傲慢", "url":"default/23.gif"},{"id":"24", "phrase":"饥饿", "url":"default/24.gif"},{"id":"25", "phrase":"困", "url":"default/25.gif"},{"id":"26", "phrase":"惊恐", "url":"default/26.gif"},{"id":"27", "phrase":"流汗", "url":"default/27.gif"},{"id":"28", "phrase":"憨笑", "url":"default/28.gif"},{"id":"29", "phrase":"大兵", "url":"default/29.gif"},{"id":"30", "phrase":"奋斗", "url":"default/30.gif"},{"id":"31", "phrase":"咒骂", "url":"default/31.gif"},{"id":"32", "phrase":"疑问", "url":"default/32.gif"},{"id":"33", "phrase":"嘘", "url":"default/33.gif"},{"id":"34", "phrase":"晕", "url":"default/34.gif"},{"id":"35", "phrase":"折磨", "url":"default/35.gif"},{"id":"36", "phrase":"衰", "url":"default/36.gif"},{"id":"37", "phrase":"骷髅", "url":"default/37.gif"},{"id":"38", "phrase":"敲打", "url":"default/38.gif"},{"id":"39", "phrase":"再见", "url":"default/39.gif"},{"id":"97", "phrase":"擦汗", "url":"default/97.gif"},{"id":"98", "phrase":"抠鼻", "url":"default/98.gif"},{"id":"99", "phrase":"鼓掌", "url":"default/99.gif"},{"id":"100", "phrase":"糗大了", "url":"default/100.gif"},{"id":"101", "phrase":"坏笑", "url":"default/101.gif"},{"id":"102", "phrase":"左哼哼", "url":"default/102.gif"},{"id":"103", "phrase":"右哼哼", "url":"default/103.gif"},{"id":"104", "phrase":"哈欠", "url":"default/104.gif"},{"id":"105", "phrase":"鄙视", "url":"default/105.gif"},{"id":"106", "phrase":"委屈", "url":"default/106.gif"},{"id":"107", "phrase":"快哭了", "url":"default/107.gif"},{"id":"108", "phrase":"阴险", "url":"default/108.gif"},{"id":"109", "phrase":"亲亲", "url":"default/109.gif"},{"id":"110", "phrase":"吓", "url":"default/110.gif"},{"id":"111", "phrase":"可怜", "url":"default/111.gif"},{"id":"112", "phrase":"菜刀", "url":"default/112.gif"},{"id":"89", "phrase":"西瓜", "url":"default/89.gif"},{"id":"113", "phrase":"啤酒", "url":"default/113.gif"},{"id":"114", "phrase":"篮球", "url":"default/114.gif"},{"id":"115", "phrase":"乒乓", "url":"default/115.gif"},{"id":"60", "phrase":"咖啡", "url":"default/60.gif"},{"id":"61", "phrase":"饭", "url":"default/61.gif"},{"id":"46", "phrase":"猪头", "url":"default/46.gif"},{"id":"63", "phrase":"玫瑰", "url":"default/63.gif"},{"id":"64", "phrase":"凋谢", "url":"default/64.gif"},{"id":"116", "phrase":"示爱", "url":"default/116.gif"},{"id":"66", "phrase":"爱心", "url":"default/66.gif"},{"id":"67", "phrase":"心碎", "url":"default/67.gif"},{"id":"53", "phrase":"蛋糕", "url":"default/53.gif"},{"id":"54", "phrase":"闪电", "url":"default/54.gif"},{"id":"55", "phrase":"炸弹", "url":"default/55.gif"},{"id":"56", "phrase":"刀", "url":"default/56.gif"},{"id":"57", "phrase":"足球", "url":"default/57.gif"},{"id":"117", "phrase":"瓢虫", "url":"default/117.gif"},{"id":"59", "phrase":"便便", "url":"default/59.gif"},{"id":"75", "phrase":"月亮", "url":"default/75.gif"},{"id":"74", "phrase":"太阳", "url":"default/74.gif"},{"id":"69", "phrase":"礼物", "url":"default/69.gif"},{"id":"49", "phrase":"拥抱", "url":"default/49.gif"},{"id":"76", "phrase":"强", "url":"default/76.gif"},{"id":"77", "phrase":"弱", "url":"default/77.gif"},{"id":"78", "phrase":"握手", "url":"default/78.gif"},{"id":"79", "phrase":"胜利", "url":"default/79.gif"},{"id":"118", "phrase":"抱拳", "url":"default/118.gif"},{"id":"119", "phrase":"勾引", "url":"default/119.gif"},{"id":"120", "phrase":"拳头", "url":"default/120.gif"},{"id":"121", "phrase":"差劲", "url":"default/121.gif"},{"id":"122", "phrase":"爱你", "url":"default/122.gif"},{"id":"123", "phrase":"NO", "url":"default/123.gif"},{"id":"124", "phrase":"OK", "url":"default/124.gif"},{"id":"42", "phrase":"爱情", "url":"default/42.gif"},{"id":"85", "phrase":"飞吻", "url":"default/85.gif"},{"id":"43", "phrase":"跳跳", "url":"default/43.gif"},{"id":"41", "phrase":"发抖", "url":"default/41.gif"},{"id":"86", "phrase":"怄火", "url":"default/86.gif"},{"id":"125", "phrase":"转圈", "url":"default/125.gif"},{"id":"126", "phrase":"磕头", "url":"default/126.gif"},{"id":"127", "phrase":"回头", "url":"default/127.gif"},{"id":"128", "phrase":"跳绳", "url":"default/128.gif"},{"id":"129", "phrase":"挥手", "url":"default/129.gif"},{"id":"130", "phrase":"激动", "url":"default/130.gif"},{"id":"131", "phrase":"街舞", "url":"default/131.gif"},{"id":"132", "phrase":"献吻", "url":"default/132.gif"},{"id":"133", "phrase":"左太极", "url":"default/133.gif"},{"id":"134", "phrase":"右太极", "url":"default/134.gif"}];
$(document).click(function(){
	$('.layer').fadeOut('fast');
});

/**
 * 显示表情输入框
 * @param id 的编号
 * @param container layer存放的容器
 * @param textbox 表情插入的容器
 */
function showEmotion(id, container, textbox){
	if(!id || !container || !textbox){
		return;
	}
	var layer = document.getElementById('layer_' + id);
	if(layer){
		$(layer).fadeIn('fast');
		return;
	}
	var emHtml = '';
	for(var i = 0; i < ems.length; i++){
		emHtml += '<a href="javascript:insertEmotion(\'' + textbox + '\', \'' + ems[i].phrase + '\');" class="emotion_img" title="' + ems[i].phrase + '"><img src="' + mlog.variable.base + '/images/em/' + ems[i].url + '" alt="' + ems[i].phrase + '"></a>' + '\n';
	}
	var html = [
		'<div id="layer_' + id + '" class="layer" style="display: block;">',
		'	<div class="layer-popup-arrow em">',
		'		<ul>',
		'			<li>',
		'				<div class="layer-popup">',
		'					<div>',
		'						<div class="layer-popup-title">',
		'							<a>关闭</a>插入表情',
		'						</div>',
		'					</div>',
		emHtml,
		'					<div class="clearfix"></div>',
		'				</div>',
		'			</li>',
		'		</ul>',
		'	</div>',
		'</div>'
	].join('\n');
	$('#' + container).append(html);
}

/**
 * 插入表情
 * @param textbox
 * @param phrase
 */
function insertEmotion(textbox, phrase){
	insertContent(textbox, '[' + phrase + ']');
}

/**
 * 解析表情
 * @param s
 * @returns
 */
function resolveEmotion(s){
	if(typeof (s) != "undefined") {
		var sArr = s.match(/\[.*?\]/g);
		if(sArr){
			for(var i = 0; i < sArr.length; i++){
				for(var j = 0; j < ems.length; j++){
					if('[' + ems[j].phrase + ']' === sArr[i]){
						var reStr = "<img src=\"" + mlog.variable.base + '/images/em/' + ems[j].url + "\" height=\"22\" width=\"22\" />";
						s = s.replace(sArr[i], reStr);
					}
				}
			}
		}
	}
	return s;
}


/**
 * 插入内容
 * @param textbox
 * @param content
 */
function insertContent(textbox, content){
	if(!textbox || !content){return;}
	var inputObj = document.getElementById(textbox);
	if(!inputObj){return;}
	
	//IE support
    if (document.selection) {
        inputObj.focus();
        sel = document.selection.createRange();
        sel.text = content;
        sel.select();
    }
    //MOZILLA/NETSCAPE support
    else if (inputObj.selectionStart || inputObj.selectionStart == '0') {
    	var startPos = inputObj.selectionStart;
        var endPos = inputObj.selectionEnd;
        // save scrollTop before insert
        var restoreTop = inputObj.scrollTop;
        inputObj.value = inputObj.value.substring(0, startPos) + content + inputObj.value.substring(endPos, inputObj.value.length);
        if (restoreTop > 0) {
        	inputObj.scrollTop = restoreTop;
        }
        inputObj.focus();
        inputObj.selectionStart = startPos + content.length;
        inputObj.selectionEnd = startPos + content.length;
	} else {
		inputObj.value += content;
		inputObj.focus();
	}
}